home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 009 / atov_ex.arc / TATOV_EX.C < prev   
Encoding:
C/C++ Source or Header  |  1986-08-31  |  7.7 KB  |  330 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dos.h>
  4. #include <limits.h>
  5.  
  6. #define LASTLINE 23
  7.  
  8. #define LOWER_LEFT 0x1800
  9. #define LOWER_RIGHT 0x184f
  10.  
  11. int lineno = 1;
  12. #ifdef L_EX                                                    /* Long */
  13. #ifdef U_EX                                                    /* Unsigned long */
  14. char top_line[] = "Test atoul_ex";
  15. int atoul_ex(char **, unsigned long *);
  16. #else                                                            /* Signed long */
  17. char top_line[] = "Test atol_ex";
  18. int atol_ex(char **, long *);
  19. #endif                                                        /* #ifdef U_EX */
  20. #else                                                            /* Standard length */
  21. #ifdef U_EX                                                    /* Unsigned int */
  22. char top_line[] = "Test atou_ex";
  23. int atou_ex(char **, unsigned *);
  24. #else                                                            /* Signed int */
  25. char top_line[] = "Test atoi_ex";
  26. int atoi_ex(char **, int *);
  27. #endif                                                        /* #ifdef U_EX */
  28. #endif                                                        /* #ifdef L_EX     */
  29.  
  30. /* Clear screen */
  31. void cls()
  32.  
  33. {
  34.     static union REGS in = {0x600, 0, 0, LOWER_RIGHT};
  35.     union REGS out;
  36.  
  37.     (void) int86(0x10, &in, &out);
  38.     return;
  39. }
  40.  
  41. /* Set cursor to (x, y) */
  42. void curset(y, x, page)
  43. int y, x, page;
  44.  
  45. {
  46.     union REGS in, out;
  47.  
  48.     in.x.ax = 0x200;
  49.     in.x.bx = 0;
  50.     in.x.dx = (y << 8) + x;
  51.     (void) int86(0x10, &in, &out);
  52.     return;
  53.  
  54. }
  55.  
  56.  
  57. void clr_r24()
  58.  
  59. {
  60.  
  61.     union REGS in, out;
  62.     
  63.     curset(24, 0, 0);
  64.     
  65.     in.x.ax = 0x600;
  66.     in.x.bx = 0;
  67.     in.x.cx = LOWER_LEFT;
  68.     in.x.dx = LOWER_RIGHT;
  69.     (void) int86(0x10, &in, &out);
  70.     return;
  71. }
  72.  
  73. /* Get a line from stdin to buffer, max_char characters maximum */
  74. void egets(buffer, max_char)
  75. char *buffer;
  76. int max_char;
  77.  
  78. {
  79.     union REGSS sin, sout;
  80.     char inbuff[256];
  81.  
  82.     inbuff[0] = max_char;
  83.     sin.x.ax = 0x0a00;
  84.     makedv(inbuff, &sin.x.dx, &sin.x.ds);
  85.     (void) intdoss(&sin, &sout);
  86.     inbuff[2 + inbuff[1]] = NULL;
  87.     movmem(&inbuff[2], buffer, inbuff[1] + 1);
  88.     return;
  89.     
  90. }
  91.  
  92.  
  93. void new_screen()
  94.  
  95. {
  96.     cls();
  97.     curset(0, (79 - strlen(top_line)) >> 1, 0);
  98.     puts(top_line);
  99.     curset(2, 0, 0);
  100.     printf("%15s%15s%4s %s\n\n", "Input String", "Number", "Ofs", "Result");
  101.     lineno = 5;
  102. }
  103.  
  104. void header()
  105.  
  106. {
  107.     clr_r24();
  108.     fputs("Press any key to continue ", stdout);
  109.     (void) getch();
  110.     new_screen();
  111. }
  112.  
  113. int test_atov_ex(p, end_char)
  114. char **p;
  115. char *end_char;
  116.  
  117. {
  118.     static char *result_name[] = {"End of non-empty string",
  119.                                             "Non-numeric character after number",
  120.                                             "Empty input string",
  121.                                             "No digits before non-numeric character",
  122.                                             "Overflow"};
  123.  
  124.     int result;
  125.     char *p1, p_disp[18];
  126. #ifdef L_EX                                                    /* Long */
  127. #ifdef U_EX                                                    /* Unsigned long */
  128.     unsigned long number;
  129. #define ATOV_EX atoul_ex
  130. #define NFORM "%17s%15lu"
  131. #else                                                            /* Signed long */
  132.     long number;
  133. #define ATOV_EX atol_ex
  134. #define NFORM "%17s%15ld"
  135. #endif                                                        /* #ifdef U_EX */
  136. #else                                                            /* Standard length */
  137. #ifdef U_EX                                                    /* Unsigned int */
  138.     unsigned number;
  139. #define ATOV_EX atou_ex
  140. #define NFORM "%17s%15u"
  141. #else                                                            /* Signed int */
  142.     int number;
  143. #define ATOV_EX atoi_ex
  144. #define NFORM "%17s%15d"
  145. #endif                                                        /* #ifdef U_EX */
  146. #endif                                                        /* #ifdef L_EX     */
  147.     int term_off;
  148.  
  149.     (void) strcpy(p_disp, "\"");
  150.     (void) strcat(strcat(p_disp, *p), "\"");
  151.  
  152.     p1 = *p;
  153.     if (lineno > LASTLINE)
  154.         header();
  155.     curset(lineno - 1, 0, 0);
  156.     result = ATOV_EX(&p1, &number);
  157.     *end_char = *p1;
  158.     if ((result != 3) ||                            /* Not a non-numeric string */
  159.          (toupper(*end_char) != 'Q'))
  160.     {
  161.         printf(NFORM, p_disp, number);
  162.         term_off = p1 - *p;
  163.         printf("%4d %d (%s)\n", term_off, result, result_name[result]);
  164.     }
  165.     ++lineno;
  166.     return(result);
  167. }
  168.  
  169. void convert_and_back(integer)
  170. #ifdef L_EX                                                    /* Long */
  171. #ifdef U_EX                                                    /* Unsigned long */
  172.     #define STC    stcul_d
  173.     unsigned long integer;
  174. #else                                                            /* Signed long */
  175.     #define STC    stcl_d
  176.     long integer;
  177. #endif                                                        /* #ifdef U_EX */
  178. #else                                                            /* Standard length */
  179. #ifdef U_EX                                                    /* Unsigned int */
  180.     #define STC    stcu_d
  181.     unsigned integer;
  182. #else                                                            /* Signed int */
  183.     #define STC    stci_d
  184.     int integer;
  185. #endif                                                        /* #ifdef U_EX */
  186. #endif                                                        /* #ifdef L_EX     */
  187.  
  188.     {
  189.     static char s[16];
  190.     static char *ps1 = s;
  191.     static char **ps2 = &ps1;
  192.     char last_char;
  193.  
  194.     STC(s, integer);
  195.         test_atov_ex(ps2, &last_char);
  196.     }
  197.  
  198. main()
  199. {
  200.     char str_number[16];
  201.     char *p0, **p00, term_char;
  202.     int i, again;
  203. #ifdef L_EX                                                    /* Long */
  204. #ifdef U_EX                                                    /* Unsigned long */
  205. #define MAX_VALUE ULONG_MAX
  206.     static unsigned long power_of_2 = 0;
  207. #else                                                            /* Signed long */
  208. #define MAX_VALUE LONG_MAX
  209.     static long power_of_2 = 0;
  210. #endif                                                        /* #ifdef U_EX */
  211. #else                                                            /* Standard length */
  212. #ifdef U_EX                                                    /* Unsigned int */
  213. #define MAX_VALUE UINT_MAX
  214.     static unsigned power_of_2 = 0;
  215. #else                                                            /* Signed int */
  216. #define MAX_VALUE INT_MAX
  217.     static int power_of_2 = 0;
  218. #endif                                                        /* #ifdef U_EX */
  219. #endif                                                        /* #ifdef L_EX     */
  220.  
  221. #ifdef U_EX                                                    /* Unsigned */
  222.     static char *over_type[] = {"Third shift", "Second add"};
  223.     static int n_over = 2;
  224. #ifdef L_EX                                                    /* Unsigned long */
  225.     static char *over_values[] = {"5368709120", "4294967296"};
  226. #else                                                            /* Unsigned int */
  227.     static char *over_values[] = {"81920", "65536"};
  228. #endif                                                        /* #ifdef L_EX */
  229. #else                                                            /* Signed */
  230.     static char *over_type[] = {"Positive", "Negative", "Big"};
  231.     static int n_over = 3;
  232. #ifdef L_EX                                                    /* Signed long */
  233.     static char *over_values[] = {"2147483648", "-2147483649", "4294967296"};
  234. #else                                                            /* Signed int */
  235.     static char *over_values[] = {"32768", "-32769", "65536"};
  236. #endif                                                        /* #ifdef L_EX */
  237. #endif                                                        /* #ifdef U_EX  */
  238.  
  239.     new_screen();
  240.  
  241.     convert_and_back(power_of_2);                /* "0" */
  242.     while (power_of_2 < MAX_VALUE)
  243.     {
  244.         power_of_2 = (power_of_2 << 1) + 1;    /* (2**n) - 1 */
  245.         convert_and_back(power_of_2);
  246. #ifndef U_EX                                        /* Signed */
  247.         convert_and_back(-power_of_2);
  248. #endif                                                /* #ifndef U_EX */
  249.         if (power_of_2 < MAX_VALUE)            /* MAX_VALUE is always odd */
  250.             convert_and_back(power_of_2 + 1);
  251. #ifndef U_EX
  252.         convert_and_back((-power_of_2) - 1);
  253. #endif                                                /* #ifndef U_EX */
  254.     }
  255.  
  256.     p00 = &p0;
  257.     header();
  258.     printf("\nOverflow cases\n");
  259.     lineno += 2;
  260.     for (i = 0; i < n_over; i++)
  261.     {
  262.         printf("\n%s\n", over_type[i]);
  263.         lineno += 2;
  264.         p0 = over_values[i];
  265.         (void) test_atov_ex(p00, &term_char);
  266.     }
  267.  
  268.     puts("\nOther terminating conditions\n\nEmpty string");
  269.     lineno += 4;
  270.     *p0 = NULL;
  271.     (void) test_atov_ex(p00, term_char);
  272.  
  273.     if (lineno > LASTLINE - 3)
  274.         header();
  275.     lineno += 2;
  276.     puts("\nNumber ends inside string");
  277.      p0 = "0q";
  278.     (void) test_atov_ex(p00, term_char);
  279.  
  280.     if (lineno > LASTLINE - 3)
  281.         header();
  282.     lineno += 2;
  283.     puts("\nEmpty number inside string");
  284.     p0 = ";";
  285.     (void) test_atov_ex(p00, term_char);
  286.  
  287.     if (lineno > LASTLINE - 5)
  288.         header();
  289.     lineno += 4;
  290.     puts("\nLeading conditions\n\nOne space");
  291.     p0 = " 1";
  292.     (void) test_atov_ex(p00, term_char);
  293.  
  294.     if (lineno > LASTLINE - 3)
  295.         header();
  296.     lineno += 2;
  297.     puts("\nTwo leading spaces");
  298.     p0 = "  6";
  299.     (void) test_atov_ex(p00, term_char);
  300.  
  301.     if (lineno > LASTLINE - 3)
  302.         header();
  303.     lineno += 2;
  304.     puts("\nOne leading space and a plus sign");
  305.     p0 = " +15";
  306.     (void) test_atov_ex(p00, term_char);
  307.  
  308.     if (lineno > LASTLINE - 3)
  309.         header();
  310.     lineno += 2;
  311.     puts("\nLeading plus sign");
  312.     p0 = "+1000";
  313.     (void) test_atov_ex(p00, term_char);
  314.  
  315.     header();
  316.     p0 = str_number;
  317.     again = 1;
  318.  
  319.  
  320.     while (again)
  321.     {
  322.         clr_r24();
  323.         fputs("Enter the string to be tested or q to quit: ", stdout);
  324.         egets(str_number, sizeof str_number);
  325.         if (test_atov_ex(p00, &term_char) == 3)            /* No numeric characters */
  326.             again = (toupper(term_char) != 'Q');
  327.     }
  328.     clr_r24(0);
  329. }
  330.